import datetime

from django.db import transaction
from django.db.models import F

from social.rank.models import Rank
from social.utils.celery_app import app


@app.task()
def add_gift_record_rank(gift_record_id):
    from social.gift.models import GiftRecord
    gift_record = GiftRecord.objects.select_related('gift').get(id=gift_record_id)
    if not gift_record.is_ranked:
        with transaction.atomic():
            # 消费榜
            keys = [Rank.key_day_out(gift_record.created_at), Rank.key_week_out(gift_record.created_at),
                    Rank.key_month_out(gift_record.created_at)]
            for x in keys:
                rank, _ = Rank.objects.get_or_create(key=x, user_id=gift_record.from_user_id)
                rank.coin = F('coin') + (gift_record.gift.coin * gift_record.count)
                rank.save(update_fields=['coin'])

            # 收入榜
            keys = [Rank.key_day_in(gift_record.created_at),
                    Rank.key_week_in(gift_record.created_at),
                    Rank.key_month_in(gift_record.created_at)]
            for x in keys:
                rank, _ = Rank.objects.get_or_create(key=x, user_id=gift_record.to_user_id)
                rank.coin = F('coin') + (gift_record.gift.coin * gift_record.count)
                rank.save(update_fields=['coin'])
            gift_record.is_ranked = True
            gift_record.save(update_fields=['is_ranked'])


@app.task()
def add_av_call_ranked(call_id):
    from social.call.models import AVCall
    from social.user.models import UserCallData
    call = AVCall.objects.get(id=call_id)

    if not call.ranked and call.status in [AVCall.OVER, AVCall.ABNORMAL_OVER]:
        with transaction.atomic():
            # 消费榜
            if call.who_pay == 'from':
                pay_user = call.from_user_id
                income_user = call.to_user_id
            else:
                pay_user = call.to_user_id
                income_user = call.from_user_id
            keys = [Rank.key_day_out(call.created_at), Rank.key_week_out(call.created_at),
                    Rank.key_month_out(call.created_at)]
            for x in keys:
                rank, _ = Rank.objects.get_or_create(key=x, user_id=pay_user)
                rank.coin = F('coin') + call.total_coin
                rank.save(update_fields=['coin'])

            # 收入榜
            keys = [Rank.key_day_in(call.created_at),
                    Rank.key_week_in(call.created_at),
                    Rank.key_month_in(call.created_at)]
            for x in keys:
                rank, _ = Rank.objects.get_or_create(key=x, user_id=income_user)
                rank.coin = F('coin') + call.total_coin
                rank.save(update_fields=['coin'])

            # 用户通话时长统计
            if call.status in [AVCall.OVER, AVCall.ABNORMAL_OVER]:
                user_call_data, _ = UserCallData.objects.get_or_create(user_id=income_user,
                                                                       week=UserCallData.key_week(call.created_at))
                user_call_data.call_duration = F('call_duration') + int(call.duration / 60)
                user_call_data.save(update_fields=['call_duration'])
            call.ranked = True
            call.save(update_fields=['ranked'])
